const app = getApp()
const WXAPI = require("../../api/newApi")
const WXAPIOLD = require("../../api/api")
import NumberAnimate from "../../utils/NumberAnimate";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    surplus_bubble_ls: [],
    bubble_max_num: 7, //气泡数量
    bubble_pos_ls: [{
        x: '10%',
        y: '8%',
      },
      {
        x: '8%',
        y: '34%',
      },
      {
        x: '15%',
        y: '60%',
      },
      {
        x: '40%',
        y: '65%',
      },
      {
        x: '80%',
        y: '12%',
      },
      {
        x: '80%',
        y: '37%',
      },
      {
        x: '70%',
        y: '63%',
      },
    ],
    isgetClick: false, //是否可点击(收取fb)
    fb: 0,
    fmf: 0,
    isDoudong: false,
    isToken: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight: app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom
    })
    console.log(app.gettop.height)
    var token = wx.getStorageSync('token')
    if(token != ''){
      this.getassetInfo()
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.login_dialog = this.selectComponent("#login_dialog");
    var token = wx.getStorageSync('token')
    console.log("555555555555", token)
    if (token == '') {
      // this.login_dialog.loginShow()
      this.data.isToken = false
    } else {
      this.data.isToken = true
      this.login_dialog.loginHide()
      this.getList()
      this.getassetInfoNo()
    }
    this.getfbInfo()
    // var token = wx.getStorageSync('token')
    // console.log("555555555555", token)
    // if (token == '') {

    // } else {
    //   this.getList()
    //   this.getassetInfo()
    // }
  },
  loginSuccess() {
    this.getList()
    this.getassetInfo()
    this.getfbInfo()
    this.data.isToken = true
  },
  getList() { //获取气泡列表
    var data = {}
    WXAPI.assetCustomerList(data, (res) => {
      if (res.code == 200) {
        this.data.list = res.result
        // this.data.list = [{},{},{},{},{},{},{}]
        let bubblels = this.data.list
        let lists = []
        console.log(bubblels)
        for (let i = 0; i < bubblels.length; i++) {
          let item = bubblels[i];
          // 插入记录
          lists.push({
            id: item.id,
            name: item.receiveTypeDict,
            num: item.receiveAmount,
            receiveType: item.receiveType,
            isget: false,
            x: item.x,
            y: item.y,
          })
        }

        this.setData({
          surplus_bubble_ls: lists
        })
        // 气泡坐标列表
        let bubble_pos_ls = this.data.bubble_pos_ls;
        //  设置生成气泡
        let surplus_bubble_ls = this.data.surplus_bubble_ls;
        let bubble_ls = [];
        let max_num = surplus_bubble_ls.length > this.data.bubble_max_num ? this.data.bubble_max_num : surplus_bubble_ls.length
        for (let i = 0; i < max_num; i++) {
          bubble_ls.push({
            ...surplus_bubble_ls[i],
            ...bubble_pos_ls[i]
          });
        }
        surplus_bubble_ls.splice(0, max_num);
        this.setData({
          bubble_ls,
          surplus_bubble_ls
        })
        console.log(bubble_ls)
      }
    })

  },
  getBranch(e) { //收取气泡
    if (this.data.isgetClick) {
      return
    }
    let index = e.currentTarget.dataset.index;
    let is_get = e.currentTarget.dataset.isget
    let id = e.currentTarget.dataset.id;
    let item = e.currentTarget.dataset.item;
    if (is_get) {
      //防止重复点击
      console.log('return-------------')
      return
    }
    this.data.isgetClick = true
    // this.setData({
    //   [`bubble_ls[${index}].isget`]: true
    // })
    // return
    this.getBubbleProfit(item)
    this.setData({
      [`bubble_ls[${index}].isget`]: true
    })
    setTimeout(() => {
      this.data.isgetClick = false
    }, 2000)
    setTimeout(() => {
      this.nextBubble(index)
    }, 1000)
  },
  // 继承气泡 - 当气泡被使用之后
  nextBubble(index) {
    // index为消失的气泡下表
    // 判断是否还有剩余气泡
    let surplus_bubble_ls = this.data.surplus_bubble_ls // 剩余气泡列表
    let bubble_pos_ls = this.data.bubble_pos_ls; // 气泡位置列表
    //气泡数量不够，不在气泡
    if (surplus_bubble_ls.length <= 0) {
      return;
    }

    // 获取气泡对象
    let bubble_item = surplus_bubble_ls[0];
    // 删除剩余气泡列表获取的对象
    surplus_bubble_ls.splice(0, 1)
    // 显示获取的气泡对象
    this.setData({
      [`bubble_ls[${index}]`]: {
        ...bubble_item,
        ...bubble_pos_ls[index]
      },
      surplus_bubble_ls
    })
  },
  dong(){
    this.setData({
      isDoudong: true
    })
    console.log(this.data.isToken)
    if(this.data.isToken==false){
      this.login_dialog.loginShow()
    }else{
      wx.navigateTo({
        url: '/ecCanvas/pages/myShareholderCard/allCard/allCard',
      })
    }
    setTimeout(()=>{
      this.setData({
        isDoudong: false
      })
    },1000)
  },
  getBubbleProfit(e) { //收取气泡接口
    // setTimeout(() => { //钱包抖动动画
    //   this.getassetInfo()
    // }, 300)
    // return
    var id = e.id
    var data = {
      id: id,
      receiveType: e.receiveType
    }
    WXAPI.customerReceive(data, (res) => {
      if (res.code == 200) {
        setTimeout(() => { //钱包抖动动画
          // this.setData({
          //   isDoudong: true
          // })
          this.getassetInfo()
        }, 300)
        // setTimeout(() => {
        //   this.setData({
        //     isDoudong: false
        //   })
        // }, 2000)
        // setTimeout(() => {
        //   this.getNum()
        // }, 1000)
      }
    })
  },
  getassetInfo() { //获取资产情况(有动画)
    var data = {}
    WXAPI.assetCustomerInfo(data, (res) => {
      if (res.code == 200) {
        let num1 = res.result.fb;
        let n1 = new NumberAnimate({
          from: num1, //显示的数字
          speed: 500, // 总时间
          refreshTime: 100, //  刷新一次的时间
          decimals: 0, //小数点后的位数
          onUpdate: () => { //更新回调函数
            this.setData({
              fb: n1.tempValue,
              fmf: res.result.honeyPoints
            });
          },
          onComplete: () => { //完成回调函数
            this.setData({
              // num1Complete:" 完成了"
            });
          }
        });
      }
    })
  },
  getassetInfoNo() { //获取资产情况(无动画)
    var data = {}
    WXAPI.assetCustomerInfo(data, (res) => {
      if (res.code == 200) {
        this.setData({
          fb: res.result.fb,
          fmf: res.result.honeyPoints
        });
      }
    })
  },
  getfbInfo() {
    var data = {}
    WXAPI.fbInfo(data, (res) => {
      if (res.code == 200) {
        this.setData({
          fbInfo: res.result
        })
      }
    })
  },
  jumpWallet() { //跳转区块链钱包
    if(!this.data.isToken){
      this.login_dialog.loginShow()
      return
    }
    wx.navigateTo({
      url: '/ecCanvas/pages/blockchainWallet/blockchainWallet',
    })
  },
  jumpmyFmf() { //我的蜂蜜分
    if(!this.data.isToken){
      this.login_dialog.loginShow()
      return
    }
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/myIntegral/myIntegral',
    })
  },
  whyFb() { //
    wx.showModal({
      title: '什么是FB？',
      content: 'FB，是蜜链盟卡包的通证。中文称为“蜂币”。是依托于区块链技术，基于个人在蜜链盟活动产生的奖励，可以用于蜜链盟生态上的消费、兑换等与现金分红。用户也可以将账户中的积分、优惠券、预付卡等闲置资产通过挂单方式向其他用户置换成FB。用户通过蜜链盟卡包进行消费可获得一定的FB，FB总量有限，且每2年产出量减少一半，随着时间的推移获取难度也越来越大，前期参与更有优势。',
      confirmText: '我已知悉',
      showCancel: false,
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  whyFmf() {
    wx.showModal({
      title: '什么是蜂蜜分？',
      content: '蜂蜜分，是蜜链盟卡包的通用等价物。用户可以在积分商城中兑换商品、商家服务、商家积分等。',
      confirmText: '我已知悉',
      showCancel: false,
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //提现
  handleWithdrawal() {
    if(!this.data.isToken){
      this.login_dialog.loginShow()
      return
    }
    wx.navigateTo({
      url: '/userPageC/pages/userPersonal/withdrawal/withdrawal',
    })
  },

})